package com.tyrone.concurrent.concurrentUtils;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * CyclicBarrier 提供 getNumberWaiting（）可以获得CyclicBarrier阻塞的数量
 * isBroken()方法用来判断线程是否被中断
 *
 * @author Tyrone
 * @date 2021/12/22 20:45
 */
public class CyclicBarrierTest3 {
    static CyclicBarrier cyclicBarrier = new CyclicBarrier(2);

    public static void main(String[] args) throws Exception {
        Thread thread = new Thread(() -> {
            try {
                cyclicBarrier.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
        });
        thread.start();
        System.out.println(cyclicBarrier.getNumberWaiting());
        thread.interrupt();
        try {
            cyclicBarrier.await();
        } catch (Exception e) {
            System.out.println(cyclicBarrier.isBroken());
        }

    }
}
